package gov.nhso.prov.acc.utils;

import java.lang.reflect.Field;
import javax.servlet.http.HttpServletRequest;

@SuppressWarnings("rawtypes")
public class BeanUtil {
	public void copyRequest2Bean(HttpServletRequest request, Object obj) {
		Class clazz = obj.getClass();
		Field[] f = clazz.getDeclaredFields();
		boolean isSet = true;
		for (int i = 0; i < f.length; i++) {
			Field f2 = f[i];
			if (request.getAttribute(f2.getName()) != null) {
				f2.setAccessible(true);
				isSet = this.setObj(f2, obj, request.getAttribute(f2.getName()));
				f2.setAccessible(false);
			}
			/*if (request.getParameter(f2.getName()) != null) {
				f2.setAccessible(true);
				isSet = this.setObj(f2, obj, this.encodeUTF((String) request.getParameter(f2.getName())));
				f2.setAccessible(false);
			}*/
			if (!isSet) {
				System.out.println(clazz.getName() + " cannot set field " + f2.getName());
			}
		}
	}
	
	private boolean setObj(Field f, Object obj, Object value) {
		boolean isSet = true;
		try {
			f.set(obj, value);
		} catch (Exception e) {
			isSet = false;
		}
		return isSet;
	}
}